package com.qcby.llmtchstd.service.Impl;

import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.dao.NoteDao;
import com.qcby.llmtchstd.entity.Note;
import com.qcby.llmtchstd.entity.Student;
import com.qcby.llmtchstd.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private NoteDao noteDao;


    @Override
    public ResponseResult count(Integer id) {
        try {
            Integer num = noteDao.count(id);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询笔记数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult page(Note note) {

        try {
            List<Note> list=noteDao.page(note);
            return ResponseResult.success(list);
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询笔记数量失败");
        }
    }

    @Override
    public ResponseResult search(Note note) {
        try {
            List<Note> list=noteDao.search(note);
            if(list.size()!=0){
                return ResponseResult.success(list);
            }
            return ResponseResult.fail("4003","笔记不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询笔记数量失败");
        }
    }

    @Override
    public ResponseResult serCount(Note note) {
        try {
            Integer num = noteDao.serCount(note);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询笔记数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult delete(Integer id) {
        try{
            int num=noteDao.delete(id);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","删除笔记失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：删除笔记数量失败");
        }
    }

    @Override
    public ResponseResult insert(Note note) {
        try {
            int num = noteDao.insert(note);
            if (num == 1) {
                return ResponseResult.success();
            }
            return ResponseResult.fail("4004", "添加笔记失败，请检查数据");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常到控制台
            return ResponseResult.dbError("数据库异常：添加笔记失败");
        }
    }

    @Override
    public ResponseResult update(Note note) {
        try {
            int num = noteDao.update(note);
            if (num == 1) {
                return ResponseResult.success();
            }
            return ResponseResult.fail("4005", "更新笔记失败，请检查数据");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常到控制台
            return ResponseResult.dbError("数据库异常：更新笔记失败");
        }
    }

    @Override
    public ResponseResult edit(Integer id) {
        try {
            Note note = noteDao.edit(id);
            if (note != null) {
                return ResponseResult.success(note);
            }
            return ResponseResult.fail("4006", "未找到该笔记");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.dbError("数据库异常：获取笔记失败");
        }
    }

    @Override
    public ResponseResult selectById(int id) {
        try {
            Note note = noteDao.selectById(id);
            if (note != null) {
                return ResponseResult.success(note);
            }
            return ResponseResult.fail("4006", "未找到该笔记");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.dbError("数据库异常：获取笔记详情失败");
        }
    }



}
